package club.gggd.qqrobot.listener;

import love.forte.simboot.annotation.Filter;
import love.forte.simboot.annotation.FilterValue;
import love.forte.simboot.annotation.Listener;
import love.forte.simboot.filter.MatchType;
import love.forte.simbot.event.GroupMessageEvent;
import love.forte.simbot.message.Image;
import love.forte.simbot.resources.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.*;

/**
 * @Description 角色攻略
 * @Author srx
 * @date 2022/9/6 14:36
 */
@Component
public class StrategyListener {

    @Value("${path.strategy}")
    private String path;

    /**
     * 图片后缀，在此要求为 jpg 格式
     */
    private final String SUFFIX = ".jpg";

    @Listener
    @Filter(value = "#{{name}}攻略", matchType = MatchType.REGEX_MATCHES)
    public void listen(GroupMessageEvent event, @FilterValue("name") String name) {
        File file = getRoleStrategy(name);
        if(file != null){
            Image<?> image = event.getBot().uploadImageBlocking(Resource.of(file));
            event.getGroup().sendBlocking(image);
        }
    }

    /**
     * 获取对应的角色攻略图片
     * @param name
     * @return
     */
    private File getRoleStrategy(String name){
        File file = new File(path + File.separatorChar + name + SUFFIX);
        if(file.exists()){
            return file;
        }
        return null;
    }
}
